<?php
/**
 *CloudDBInterface.php
 */

/**
 * Interface for CloudDB to abstract REST api.
 *
 * @author patrickayoup
 */
class CloudDBInterface {

    /**
     * API Key
     * @var String The key which gives us access to the API 
     */
    private static $key = 'yzk8g08x';
    
    /**
     * Makes a request to the server.
     * @param String $url The complete url to request.
     * @return String The return from the server. (Plain Text, JSON or XML)
     */
    private function makeRequest($url) {
        
        //Make request and return.
        $curl_handle=curl_init();
        curl_setopt($curl_handle,CURLOPT_URL,$url);
        curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
        curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
        curl_setopt ($curl_handle, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt ($curl_handle,CURLOPT_SSL_VERIFYPEER, 0);
        $result = htmlspecialchars(curl_exec($curl_handle));
        curl_close($curl_handle);
        
        return $result;
    }
    
    /**
     * Creates a table in CloudDB.
     * @param String $query The query in plain SQL.
     * @return String Message indicating Success or Fail. 
     */
    public function createTable($query) {

        $api = "https://sepehr.cs.concordia.ca:8081/CloudDB/resources/API/";
        $url = $api."ctable/key/".self::$key."/".rawurlencode($query);

        return $this->makeRequest($url);
    }
    
    /**
     * Deletes a table in CloudDB.
     * @param String $query The query in plain SQL.
     * @return String Message indicating Success or Fail.
     */
    public function deleteTable($query) {
        
        $api = "https://sepehr.cs.concordia.ca:8081/CloudDB/resources/API/";
        $url = $api."dtable/key/".self::$key."/".rawurlencode($query);
        
        return $this->makeRequest($url);
    }
    
    /**
     * Makes a SELECT query in CloudDB.
     * @param String $query The query in plain SQL.
     * @param String $format The return format (json or xml).
     * @return String Results in requested format.
     */
    public function selectQuery($query) {

        $format = 'json';
        $api = 'https://sepehr.cs.concordia.ca:8081/CloudDB/resources/API/';
        $url = $api.'select/format/'.$format.'/key/'.self::$key.'/'.rawurlencode($query);

        $result = $this->makeRequest($url);

        $result2 = $this->parseJSON($result);

        return $result2;
    }
    
    /**
     * Makes an INSERT query in CloudDB.
     * @param String $query The query in plain SQL.
     * @return String Message indicating Success or Fail.
     */
    public function insertQuery($query) {
        
        $api = "https://sepehr.cs.concordia.ca:8081/CloudDB/resources/API/";
        $url = $api."insert/key/".self::$key."/".rawurlencode($query);
        
        return $this->makeRequest($url);
    }
    
    /**
     * Makes an UPDATE query in CloudDB.
     * @param String $query The query in plain SQL.
     * @return String Message indicating Success or Fail.
     */
    public function updateQuery($query) {
        
        $api = "https://sepehr.cs.concordia.ca:8081/CloudDB/resources/API/";
        $url = $api."update/key/".self::$key."/".rawurlencode($query);
        
        return $this->makeRequest($url);
    }
    
    /**
     * Makes an ALTER query in CloudDB.
     * @param String $query The query in plain SQL.
     * @return String Message indicating Success or Fail.
     */
    public function alterQuery($query) {
        
        $api = "https://sepehr.cs.concordia.ca:8081/CloudDB/resources/API/";
        $url = $api."alter/key/".self::$key."/".rawurlencode($query);
        
        return $this->makeRequest($url);
    }
    
    /**
     * Lists all tables in CloudDB. Usual query is "show tables"
     * @param String $query The query in plain SQL.
     * @param String $format The return format (json or xml).
     * @return String Results in requested format.
     */
    public function listTables() {
        
        $query = "show tables";
        $format = "json";
        $api = "https://sepehr.cs.concordia.ca:8081/CloudDB/resources/API/";
        $url = $api."ltables/format/".$format."/key/".self::$key."/".rawurlencode($query);
        
        $result = $this->makeRequest($url);

        $result2 = $this->parseJSON($result);

        return $result2;
    }
    
    /**
     * Performes a TRUNCATE query in CloudDB.
     * @param String $query The query in plain SQL.
     * @return String Message indicating Success or Fail.
     */
    public function truncateTable($query) {
     
        $api = "https://sepehr.cs.concordia.ca:8081/CloudDB/resources/API/";
        $url = $api."truncate/key/".self::$key."/".rawurlencode($query);
        
        return $this->makeRequest($url);
    }
       
    /**
     * Parses JSON result.
     * @param String $jsonResult JSON representation of result.
     * @return Array of Associative arrays representing each tuple.
     */
    public function parseJSON($jsonResult) {
        
        //Convert from HTML entities to plain text.
        $out = html_entity_decode($jsonResult);
        
        //Parse JSON.
        $result = json_decode($out,true);
      
        //Return the data in the form of an array of associative arrays representing a tuple.
        return $result;
    }
}
?>